/* eslint-disable no-mixed-spaces-and-tabs */
/******************************************************************/
/**
 * @description 增加px自动转换rem逻辑
 */
/******************************************************************/

const path = require('path')
const autoprefixer = require('autoprefixer')
const px2rem = require('postcss-plugin-px2rem')
const _resolve = (dir) => path.join(__dirname, dir)
module.exports = ({ file }) => {
	if (!file?.dirname) {
		return
	}
	let remUnit = 0
	if (file.dirname.startsWith(_resolve(`/src`))) {
		// 本地插件库(不转换样式)
		if (file.dirname.startsWith(_resolve(`/src/libs`))) {
			remUnit = 0
		} else {
			remUnit = 75
		}
	} else if (file.dirname.startsWith(_resolve(`/node_modules/vant/`))) {
		// 第三方依赖只对vant处理
		remUnit = 37.5
	}

	return {
		plugins: remUnit
			? [
					autoprefixer(),
					px2rem({
						rootValue: remUnit,
						propList: ['*'],
						selectorBlackList: ['ignore-rem'],
						minPixelValue: 2, // 最小转换像素值，包含2px
					}),
			  ]
			: [autoprefixer()],
	}
}
